package com.example.yue.appclent.client;

import android.widget.TextView;

/**
 * @author Yue
 * @date 2017/8/16 12:04
 */
public class TvLog<LogView extends TextView> {
    private int limit = 100;
    private LogView logView;
    private int logCount;

    private TvLog(LogView logView) {
        this.logView = logView;
    }

    public static <LogView extends TextView> TvLog newInstance(LogView logView) {
        return new TvLog(logView);
    }

    private TvLog(LogView logView, int limit) {
        this.logView = logView;
        this.limit = limit;
    }

    public static <LogView extends TextView> TvLog newInstance(LogView logView, int limit) {
        return new TvLog(logView, limit);
    }

    public void log(CharSequence text) {
        if (logView != null) {
            logView.append(text + "\n");
            logCount++;
            tryReset(text);
        }
    }

    private void tryReset(CharSequence text) {
        if (logCount >= limit) {
            if (logView != null) {
                logView.setText(text);
            }
            logCount = 0;
        }
    }

    public void clear() {
        if (logView != null) {
            logView.setText("");
        }
    }
    public void release(){
        logView = null;
    }
}
